package com.xinsoft.controller.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.controller.service.ScmPurchaseScmSaleService;
import com.xinsoft.entity.dto.ScmPurchaseDto;
import com.xinsoft.entity.dto.ScmPurchaseItemParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.entity.vo.ScmPurchaseItemVo;
import com.xinsoft.entity.vo.ScmPurchaseVo;
import com.xinsoft.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScmPurchaseScmSaleServiceImpl implements ScmPurchaseScmSaleService {

    @Autowired
    private ScmDocumentService scmDocumentService;
    @Autowired
    private ScmPurchaseItemService scmPurchaseItemService;
    @Autowired
    private SysFileHisService sysFileHisService;
    @Autowired
    private FmsAccountsPayService fmsAccountsPayService;
    @Resource
    SysFileHisService fileHisService;
    @Resource
    ScmPurchaseService scmPurchaseService;
    @Autowired
    private ScmPurchasePlanService scmPurchasePlanService;
    @Autowired
    private SysDocumentNumService sysDocumentNumService;


    @Override
    public Response<?> submintApprove(ScmPurchaseDto scmPurchaseDto) {
        Integer id = this.addOrUpdate(scmPurchaseDto);
        //2、提交审批
        scmPurchasePlanService.approveNodeAdd(scmPurchaseDto.getPurchaseCode(), id, Const.ApproveType.CGDD,1, Const.ApproveTypeName.CGDD);
        return Response.succeed();
    }

    @Override
    public Integer addOrUpdate(ScmPurchaseDto scmPurchaseDto) {
        ScmPurchase scmPurchase = new ScmPurchase();
        BeanUtils.copyProperties(scmPurchaseDto,scmPurchase);
        //1、新增/修改采购申请
        if (ObjectUtil.isEmpty(scmPurchaseDto.getId())){
            // 更新计划单号
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.PURCHASE_DOCUMENT);
            scmPurchaseService.save(scmPurchase);
        }else {
            scmPurchaseService.updateById(scmPurchase);
            scmPurchaseItemService.remove(new LambdaQueryWrapper<ScmPurchaseItem>().eq(ScmPurchaseItem::getPurchaseId,scmPurchase.getId()));
            fmsAccountsPayService.remove(new LambdaQueryWrapper<FmsAccountsPay>().eq(FmsAccountsPay::getPurchaseId, scmPurchase.getId()));
            scmDocumentService.remove(new LambdaQueryWrapper<ScmDocument>().eq(ScmDocument::getScmId, scmPurchase.getId())
                    .eq(ScmDocument::getScmType, Const.ApproveType.CGDD));
        }

        List<SysFileHis> sysFileHisList = scmPurchaseDto.getSysFileHisList();
        List<FmsAccountsPay> fmsAccountsPays = scmPurchaseDto.getFmsAccountsReceivableList();
        List<ScmPurchaseItem> itemList = scmPurchaseDto.getItemList();
        if (ObjectUtil.isNotEmpty(sysFileHisList)){
            //附件操作、文档操作
            sysFileHisService.saveBatch(sysFileHisList);
            this.saveDocumentList(sysFileHisList, Const.ApproveType.CGDD, scmPurchase.getId());
        }
        if (ObjectUtil.isNotEmpty(fmsAccountsPays)){
            fmsAccountsPays.forEach(item ->{
                item.setPurchaseId(scmPurchase.getId());
            });
            //应付账款操作
            fmsAccountsPayService.saveBatch(fmsAccountsPays);
        }
        if (ObjectUtil.isNotEmpty(itemList)){
            itemList.forEach(item ->{
                item.setPurchaseId(scmPurchase.getId());
                item.setDoClose(0);
                item.setDoOvercharge(0);
                item.setDoCheck(0);
            });
            scmPurchaseItemService.saveBatch(itemList);
        }
        return scmPurchase.getId();
    }
    @Override
    public ScmPurchaseVo getScmPurchase(Integer id) {
        ScmPurchaseVo purchasePlanVo = scmPurchaseService.getScmPurchase(id);
        ScmPurchaseItemParam itemParam = new ScmPurchaseItemParam();
        itemParam.setPurchaseId(id);
        List<ScmPurchaseItemVo> purchaseItemList = scmPurchaseItemService.listPurchaseItem(itemParam);
        purchaseItemList.forEach(item ->{
            item.setViceNum((ObjectUtil.isEmpty(item.getNeedNum()) || item.getNeedNum().equals(BigDecimal.ZERO) || ObjectUtil.isEmpty(item.getConvCoefficient())) ?
                    BigDecimal.ZERO : item.getNeedNum().multiply(item.getConvCoefficient()));
        });
        purchasePlanVo.setItemList(purchaseItemList);
        List<ScmDocument> documents = scmDocumentService.list(new LambdaQueryWrapper<ScmDocument>().eq(ScmDocument::getScmType, Const.ApproveType.CGDD)
                .eq(ScmDocument::getScmId, id));
        if (ObjectUtil.isNotEmpty(documents)){
            List<Integer> documentIds = documents.stream().map(ScmDocument::getDocumentId).collect(Collectors.toList());
            List<SysFileHis> sysFileHis = fileHisService.listByIds(documentIds);
            purchasePlanVo.setSysFileHisList(sysFileHis);
        }
        List<FmsAccountsPay> payList = scmPurchaseService.getScmPayById(id);
        purchasePlanVo.setFmsAccountsReceivableList(payList);
        return purchasePlanVo;
    }
    /**
     * 新增文档记录
     * @param cusDocumentList
     * @param type
     * @param sourceId
     */
    public void saveDocumentList(List<SysFileHis> cusDocumentList, Integer type, Integer sourceId) {
        if (ObjectUtil.isEmpty(cusDocumentList)){
            return;
        }
        List<ScmDocument> scmDocuments = cusDocumentList.stream().map(item -> {
            ScmDocument scmDocument = new ScmDocument();
            scmDocument.setScmId(sourceId);
            scmDocument.setScmType(type);
            scmDocument.setDocumentId(item.getId());
            return scmDocument;
        }).collect(Collectors.toList());
        if(ObjectUtil.isNotEmpty(scmDocuments)){
            scmDocumentService.saveBatch(scmDocuments);
        }
    }
}
